﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Components_Right : System.Web.UI.UserControl
{
    DBDataContext db;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindShoppingCart();
            SupportOnline();
        }
    }

    private void bindShoppingCart()
    {
        if (Session["ShoppingCart"] != null)//Shopping Cart co san pham
        {
            DataTable dt = (DataTable)Session["ShoppingCart"];
            lvShoppingCart.DataSource = dt;
            lvShoppingCart.DataBind();
            span_ShoppingCount.InnerText = String.Format("{0:#,#.##}", dt.Rows.Count);
            span_ShoppingCartTotalAmount.InnerText = String.Format("{0:#,#.##}", dt.AsEnumerable().Sum(dr => Convert.ToDouble(dr["Amount"])));

        }
        else//Shopping Cart khong co san pham
        {
            span_ShoppingCount.InnerText = "0";
            span_ShoppingCartTotalAmount.InnerText = "0";
            btnPayment.Visible = false;
        }
    }
    private void SupportOnline()
    {
        try
        {
            db = LINQUtil.GetDBWriteOnly();
            var query = (from a in db.Employees
                       where a.IsAdvisor == true && a.Status == 'A'
                       select new
                       {
                           EmplyeeName= a.EmplyeeName,
                           PhoneNumerComp = a.PhoneNumberComp,
                           Yahoo = a.Yahoo,
                           Skype = a.Skype == null ? "thuynguyen12488" : a.Skype
                       }).ToList();
            if (query != null)
            {
                DataTable dt = query.ToDataTable();
                lvSupport.DataSource = dt;
                lvSupport.DataBind();
            }
            else { 
                div_support.Style["Display"] = "None";
            }
        }
        catch (Exception ex)
        {
            ControlUtil.LogError("Error SupportOnline Page Right:" + ex.Message);
        }
    }
    protected void btnPayment_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/don-dat-hang", false);
    }
    protected void ImgbtnDelProduct_Command(object sender, CommandEventArgs e)
    {
        try
        {
            //Xoa san pham ra khoi gio hang
            if (e.CommandArgument != null)
            {
                /*TODO*/
                string strProductID = e.CommandArgument.ToString();
                DataTable dt = (DataTable)Session["ShoppingCart"];
                DataRow[] rows = dt.Select("ProductID=" + Convert.ToInt32(strProductID));
                foreach (DataRow dr in rows)
                {
                    dr.Delete();
                }
                if (dt.Rows.Count > 0)
                {
                    //span_ShoppingCount.InnerText = String.Format("{0:#,#.##}", dt.Rows.Count);
                    //span_ShoppingCartTotalAmount.InnerText = String.Format("{0:#,#.##}", dt.AsEnumerable().Sum(dr => Convert.ToDouble(dr["Amount"])));
                    bindShoppingCart();
                }
                else
                {
                    span_ShoppingCount.InnerText = "0";
                    span_ShoppingCartTotalAmount.InnerText = "0";
                    btnPayment.Visible = false;
                    Session.Remove("ShoppingCart");
                }
                Response.Redirect(Request.UrlReferrer.AbsolutePath, false);
            }
            else
            {
                /*TODO*/
                ControlUtil.LogError("Error Delete Product From Shopping Cart Page Right");
            }
        }
        catch (Exception ex)
        {
            ControlUtil.LogError("Error Delete Product From Shopping Cart Page Right:" + ex.Message.ToString());
        }
    }
}